<!DOCTYPE html>
<html lang="fr">

<head>
  <meta charset="UTF-8">
  <title>dialog-connector</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;600;800&display=swap" rel="stylesheet">
  <style>
    :root {
      --bg: #f9f9f9;
      --text: #212529;
      --card-bg: #ffffff;
      --input-bg: #e9ecef;
      --accent: #0d6efd;
    }

    .dark-mode {
      --bg: #121212;
      --text: #f1f1f1;
      --card-bg: #1e2a38;
      --input-bg: #2a3b4d;
      --accent: #00c9a7;
    }

    html,
    body {
      min-height: 100%;
      background-color: var(--bg);
      color: var(--text);
      font-family: "Roboto", sans-serif;
    }

    .chat-bubble {
      padding: 0.5rem 1rem;
      border-radius: 1rem;
      margin-bottom: 0.5rem;
      background-color: var(--card-bg);
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      max-width: 80%;
      word-break: break-word;
    }

    .chat-bubble.user {
      background-color: var(--accent);
      color: #fff;
      align-self: flex-end;
    }

    .chat-bubble.bot {
      background-color: var(--card-bg);
      color: var(--text);
      align-self: flex-start;
    }

    .sidebar {
      background-color: var(--card-bg);
      border-right: 1px solid rgba(0, 0, 0, 0.1);
      padding: 1rem;
      height: 100%;
    }

    .conversation-list li {
      cursor: pointer;
      padding: 0.5rem;
      border-radius: 0.5rem;
    }

    .conversation-list li:hover {
      background-color: var(--input-bg);
    }

    .toggle-mode {
      position: fixed;
      top: 1rem;
      right: 1rem;
      z-index: 999;
    }

    .status-indicator {
      display: inline-block;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      margin-right: 0.5rem;
      background-color: #dc3545;
    }

    .status-connected {
      background-color: #28a745 !important;
    }

    @media (max-width: 768px) {
      .layout {
        flex-direction: column;
      }

      .sidebar {
        border-right: none;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        height: auto;
      }
    }
  </style>
</head>

<body>
  <button class="btn btn-sm btn-outline-secondary toggle-mode" onclick="toggleTheme()">Toggle thème</button>
  <div class="d-flex layout vh-100">
    <aside class="sidebar flex-shrink-0">
      <h5>Conversations</h5>
      <ul class="list-unstyled conversation-list">
        <li>Conversation 1</li>
        <li>Conversation 2</li>
      </ul>
      <button id="btnTestWS" class="btn btn-success w-100 mt-3">Tester WebSocket</button>
      <div class="alert alert-secondary mt-3 small" id="ws-response"
        style="display:none; word-break: break-all; overflow-wrap: anywhere;"></div>
    </aside>
    <main class="flex-grow-1 d-flex flex-column p-3">
      <h1 class="text-primary mb-0">dialog-connector</h1>
      <small class="text-secondary">Version HTML</small>
      <div class="d-flex align-items-center mb-2 mt-2">
        <span class="status-indicator" id="ws-status"></span>
        <small id="ws-status-text">Déconnecté</small>
      </div>
      <div id="chat-container" class="flex-grow-1 d-flex flex-column gap-2 p-2 border rounded overflow-auto">
      </div>
      <div class="mt-2 d-flex gap-2">
        <input type="text" id="messageInput" class="form-control" placeholder="Tapez votre message...">
        <button id="btnSend" class="btn btn-primary">Envoyer</button>
      </div>
    </main>
  </div>
  <script>
    function toggleTheme() {
      document.body.classList.toggle('dark-mode')
      document.documentElement.classList.toggle('dark-mode')
    }

    const wsStatus = document.getElementById('ws-status')
    const wsStatusText = document.getElementById('ws-status-text')
    const wsResponse = document.getElementById('ws-response')
    const btnTestWS = document.getElementById('btnTestWS')
    const btnSend = document.getElementById('btnSend')
    const inputMsg = document.getElementById('messageInput')
    const chatContainer = document.getElementById('chat-container')

    let ws

    function connectWS() {
      ws = new WebSocket('ws://localhost:8080')
      ws.onopen = () => {
        wsStatus.classList.add('status-connected')
        wsStatusText.textContent = 'Connecté'
      }
      ws.onclose = () => {
        wsStatus.classList.remove('status-connected')
        wsStatusText.textContent = 'Déconnecté'
      }
      ws.onerror = (err) => {
        console.error('WebSocket erreur', err)
      }
      ws.onmessage = (message) => {
        const msg = document.createElement('div')
        msg.className = 'chat-bubble bot'
        msg.textContent = message.data
        chatContainer.appendChild(msg)
        chatContainer.scrollTop = chatContainer.scrollHeight
        wsResponse.style.display = 'block'
        wsResponse.textContent = `Réponse reçue`
      }
    }

    connectWS()

    btnTestWS.addEventListener('click', () => {
      if (ws && ws.readyState === WebSocket.OPEN) {
        ws.send('Test de message')
        wsResponse.style.display = 'block'
        wsResponse.textContent = 'Message de test envoyé...'
      } else {
        wsResponse.style.display = 'block'
        wsResponse.textContent = 'WebSocket non connecté'
      }
    })

    btnSend.addEventListener('click', () => {
      const content = inputMsg.value.trim()
      if (content && ws && ws.readyState === WebSocket.OPEN) {
        const userMsg = document.createElement('div')
        userMsg.className = 'chat-bubble user'
        userMsg.textContent = content
        chatContainer.appendChild(userMsg)
        ws.send(content)
        inputMsg.value = ''
        chatContainer.scrollTop = chatContainer.scrollHeight
      }
    })

    btnSend.addEventListener('click', () => {
      const content = inputMsg.value.trim()
      if (!content) return
      if (ws && ws.readyState === WebSocket.OPEN) {
        const userMsg = document.createElement('div')
        userMsg.className = 'chat-bubble user'
        userMsg.textContent = content
        chatContainer.appendChild(userMsg)
        ws.send(content)
        inputMsg.value = ''
        chatContainer.scrollTop = chatContainer.scrollHeight
        wsResponse.style.display = 'block'
        wsResponse.textContent = `Message envoyé au serveur`
      } else {
        wsResponse.style.display = 'block'
        wsResponse.textContent = 'WebSocket non connecté'
      }
    })
  </script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>